package com.szgr.baoyin.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.model.MemberLevel;
import com.szgr.baoyin.utils.Constant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 都银卡
 */
public class VipCardActivity extends BaseActivity {

    private TextView tv_remain;
    private TextView tv_cardName;
    private TextView tv_cardLevel;
    private TextView tv_cardTime;
    private TextView tv_cardPro;
    private TextView tv_cardPei;
    private List<MemberLevel> levels;
    private String predepoit;
    private String level_validity;
    private String level_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_vip_card);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("都银卡详情");
    }

    @Override
    public void initView() {
        tv_remain = (TextView) findViewById(R.id.vip_card_remain);
        tv_cardName = (TextView) findViewById(R.id.card_name);
        tv_cardLevel = (TextView) findViewById(R.id.card_level);
        tv_cardTime = (TextView) findViewById(R.id.card_time);
        tv_cardPro = (TextView) findViewById(R.id.card_protect);
        tv_cardPei = (TextView) findViewById(R.id.card_peijian);

        findViewById(R.id.vip_apply_back).setOnClickListener(this);

        getUserMessage();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.vip_apply_back:
            case R.id.action_bar_left:
                finish();
                break;
        }
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Constant.REQUEST_SUCCESS:
                    tv_remain.setText(predepoit);
                    tv_cardTime.setText(level_validity);
                    tv_cardName.setText(level_name);
                    for (MemberLevel ml : levels) {
                        if("12".equals(ml.getStore_goods_class())){
                            tv_cardPei.setText((Double.parseDouble(ml.getLevel_discount()) * 10) + "折");
                        }else if("13".equals(ml.getStore_goods_class())){
                            tv_cardPro.setText((Double.parseDouble(ml.getLevel_discount()) * 10) + "折");
                        }
                        tv_cardLevel.setText("Lv " + ml.getLevel_id());
                    }
                    break;
            }
        }
    };

    /**
     * 获取用户信息
     */
    private void getUserMessage() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_MY_MESSAGE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("----->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            String code = jsonObject.getString("code");
                            if("200".equals(code) && jsonObject.has("datas")){
                                JSONObject datas = jsonObject.getJSONObject("datas");
                                JSONObject member_info = datas.getJSONObject("member_info");
                                predepoit = member_info.getString("predepoit");
                                JSONObject member_level = member_info.getJSONObject("member_level");
                                level_validity = member_level.getString("level_validity");
                                level_name = member_level.getString("level_name");
                                JSONArray level_discount = member_level.getJSONArray("level_discount");
                                Gson gson = new Gson();
                                levels = gson.fromJson(level_discount.toString(),new TypeToken<List<MemberLevel>>(){}.getType());
                                mHandler.obtainMessage(Constant.REQUEST_SUCCESS).sendToTarget();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key", MyApplication.getmInstance().getKey());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }
}
